package aop;

import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by zhangyanfei on 2018/8/17.
 *
 * @author zhangyanfei
 * @date 2018/08/17
 */
@Configuration
public class AppConfig {
    @Bean
    public UserService userService(){
        return new UserServiceImpl();
    }
    //创建Advice或Advisor
    @Bean
    public Advice myMethodInterceptor(){
        return new MyMethodIntercepter();
    }
    //使用BeanNameAutoProxyCreator来创建代理类
    @Bean
    public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
        BeanNameAutoProxyCreator beanNameAutoProxyCreator=new BeanNameAutoProxyCreator();
        //设置要创建代理的那些Bean的名字
        beanNameAutoProxyCreator.setBeanNames(new String[]{"userSer*"});
        //设置拦截链名字(这些拦截器是有先后顺序的)
        beanNameAutoProxyCreator.setInterceptorNames(new String[]{"myMethodInterceptor"});
        return beanNameAutoProxyCreator;
    }

}
